package com.etheart.lyric.htmlparser.service.baidu;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.springframework.stereotype.Service;

import com.etheart.model.entity.Singer;

@Service("htmlparse_BaiduSingerParseService")
public class BaiduSingerParseService {
	private static final Log logger = LogFactory
			.getLog(BaiduSingerParseService.class);
//info-a8r3dcce.shtml
	public Singer getSingerFromParser(Parser parser,String netid){
		//String netid = RegexUtils.getMatcheGroup(".*([0-9]*)\\.html",fileName);
		Singer singer = new Singer();
		singer.setNetId(netid);
		return getSinger(parser, singer);
	}
	
	private Singer getSinger(Parser parser, Singer singer) {
		// 得到歌手名称
		try {
			String singerName = parseSingerName(parser);
			singer.setSingername(singerName);
		} catch (ParserException e) {
			logger.error("qqsingerid:" + singer.getNetId()
					+ " singername:解析异常", e);
			e.printStackTrace();
		}
		
		//歌手图片
		try {
			String imgPath = parseSingerImg(parser);
			singer.setPicture(imgPath);
		} catch (ParserException e) {
			logger.error("qqsingerid:" + singer.getNetId() + " picUrl:解析异常",e);
			e.printStackTrace();
		}
		
		// 歌手简介
		try {
			String intr = parseSingerIntro(parser);
			singer.setSingerintro(intr);
		} catch (ParserException e) {
			logger.error("qqsingerid:" + singer.getNetId()
					+ " singerintro:解析异常", e);
			e.printStackTrace();
		}
		
		
		
		// 歌手排行  靠 没有
	//	int rank = parseSingerRank(parser);

		return singer;
	}





	private String parseSingerIntro(Parser parser) throws ParserException {
		parser.reset();
		NodeFilter filter = new AndFilter(new TagNameFilter("li"),
				new HasAttributeFilter("class", "unline"));
		NodeList nodeList = parser.parse(filter);
		if ( nodeList.size()<=0) {
			return null;
		}
		Tag tag = (Tag) nodeList.elementAt(0);
		String jianjie=tag.toPlainTextString().trim();
		return jianjie.substring(2, jianjie.length());
	}

	private String parseSingerImg(Parser parser) throws ParserException {
		parser.reset();
		NodeFilter imgFilter = new AndFilter( new TagNameFilter("img"),new HasAttributeFilter("height","120"));
		//得到歌手图片地址
		NodeList nodeList = parser.parse(imgFilter);
		Tag tag = (Tag) nodeList.elementAt(0);
		String src = tag.getAttribute("src");
		return src.replace("http://hiphotos.baidu.com/ting/pic", "");
		
	}

	// 解析歌手名
	private String parseSingerName(Parser parser) throws ParserException {
		parser.reset();
//		<div class="listen_singerintro_name">
//        <p><strong>Peter Buffett</strong></p>
//        <p>Peter Buffett</p>
//    </div>
		NodeFilter filter = new AndFilter(new TagNameFilter("span"),new HasAttributeFilter("class","fwb cs fl fs16p"));  
		NodeList nodeList = parser.parse(filter);
		Node node = nodeList.elementAt(0);
		return node.toPlainTextString();
	}
	
	public String getAlbumListUrl(Singer singer){
		return "http://ting.baidu.com/artist/"+singer.getNetId()+"/album";
	}
	
	public String getAlbumListFileName(Singer singer){
		return "/artist/"+singer.getNetId()+"/album.html";
	}
}
